package com.boot.mall.coupon.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.common.to.seckill.SecKillSessionTo;
import com.boot.common.to.seckill.SecKillSkuRelation;
import com.boot.mall.coupon.entity.SeckillPromotionEntity;
import com.boot.mall.coupon.entity.SeckillSkuRelationEntity;
import com.boot.mall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.common.utils.PageUtils;
import com.boot.common.utils.Query;

import com.boot.mall.coupon.dao.SeckillSessionDao;
import com.boot.mall.coupon.entity.SeckillSessionEntity;
import com.boot.mall.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils listPage(Map<String, Object> params) {
        // 获取参数
        String curr = (String) params.get("page");
        long current = 1;
        if (curr != null) {
            current = Integer.parseInt(curr);
        }
        long limit = 1;
        String limitStr = (String) params.get("limit");
        if (limitStr != null) {
            limit = Integer.parseInt(limitStr);
        }
        Page<SeckillSessionEntity> page = new Page<>(current, limit);
        baseMapper.selectPage(page,null);
        PageUtils pageUtils = new PageUtils(page.getRecords(),(int)page.getTotal(),(int)limit,(int)current);
        return pageUtils;
    }

    /**
     * 查询近三天的秒杀活动场次信息
     * @return
     */
    @Override
    public List<SecKillSessionTo> getNearThreeDaysSecKillSession() {
        String startTime = getStartTime();
        String endTime = getEndTime();
        List<SeckillSessionEntity> list = this.list(new QueryWrapper<SeckillSessionEntity>().between("start_time", startTime, endTime));
        if (list != null && list.size() > 0) {
            List<SecKillSessionTo> secKillRelationSku = list.stream().map(item -> {
                // 创建秒杀场次关联的秒杀商品对象
                SecKillSessionTo session = new SecKillSessionTo();
                BeanUtils.copyProperties(item, session);
                // 查询当前秒杀场次关联的商品信息
                List<SeckillSkuRelationEntity> skus = seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", item.getId()));
                // 类型转换成封装的类型
                List<SecKillSkuRelation> collect = skus.stream().map(relation -> {
                    SecKillSkuRelation skuRelation = new SecKillSkuRelation();
                    BeanUtils.copyProperties(relation, skuRelation);
                    return skuRelation;
                }).collect(Collectors.toList());

                session.setRelationSkuList(collect);
                return session;
            }).collect(Collectors.toList());
            return secKillRelationSku;
        }
        return null;
    }

    /**
     * 获取当前小时的最小时间 例如2021-6-9 11:35 得到时间：2021-6-9 00:00
     * @return
     */
    private String getStartTime(){
        LocalDate nowDate = LocalDate.now();    //获取当前时间
        LocalTime nowTime = LocalTime.MIN;  //今日最小时间,00:00
        LocalDateTime time = LocalDateTime.of(nowDate, nowTime);
        String format = time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }
    /**
     * 获取当前小时的最小时间 例如2021-6-9 11:35 得到时间：2021-6-9 23:59
     * @return
     */
    private String getEndTime(){
        LocalDate localDate = LocalDate.now().plusDays(2);
        LocalTime localTime = LocalTime.MAX;
        LocalDateTime time = LocalDateTime.of(localDate, localTime);
        String format = time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }

}